﻿using BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utils;

namespace BookM
{
    public partial class Management : Form
    {
        public Management()
        {
            InitializeComponent();
        }
        #region 对象
        BookManagementBLL bookManagementBLL = new BookManagementBLL();
        PagingBLL pagingBLL = new PagingBLL();
        PagingBLL Paging = new PagingBLL();
        //初始化PageDto实体类
        PageDto PageDto = new PageDto
        {
            tab = "BookCategory",
            tab1 = "BookInfo",
            column = "ID",
            column1 = "CategoryType",
            line = 100,//默认显示行
            start = 1,//获取当前页数
            details = @"b.ID 编号,b.Name 图书名称,a.CategoryName 图书类别,b.Author 作者,
                b.Money 价格,b.BookID 图书id,c.Description 借出状态",
        };
        #endregion


        /// <summary>
        /// 窗体加载时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Management_Load(object sender, EventArgs e)
        {
            //目录
            AddTvwNode();

            //图书列表
            DataTable dt = Paging.Thequery3(PageDto);
            Dgv_list.DataSource = dt;
            Dgv_list.AutoGenerateColumns = false;//不让datagridview自动生成列，可在属性中修改
            Dgv_list.AllowUserToAddRows = false;//禁止自动生成行可在属性中修改
            //在datagridview中添加button按钮
            DataGridViewButton();

        }


        #region 绑定 TrreView 、查询数据
        /// <summary>
        /// 创建父节点
        /// </summary>
        private TreeNode AddTvwNode()
        {

            TreeNode treeNode = new TreeNode();
            treeNode.Text = "图书信息管理目录";
            treeNode.Name = "";
            //根目录
            tree_Role.Nodes.Add(treeNode);

            //父节点数据循环
            DataTable tb = bookManagementBLL.FindParent();

            foreach (DataRow dr in tb.Rows)
            {
                TreeNode tn = new TreeNode();
                if (dr["ParentID"].ToString() == "0")
                {
                    tn.Text = dr["CategoryName"].ToString();
                    tn.Name = "";
                    tn.Tag = dr["ID"].ToString();
                    tree_Role.Nodes[0].Nodes.Add(tn);
                }
                //创建子节点
                NewNodes(dr, tn);
            }
            return treeNode;
        }

        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="tn"></param>
        private void NewNodes(DataRow dr, TreeNode tn)
        {
            //子节点数据循环
            foreach (DataRow drr in bookManagementBLL.FindCategory().Rows)
            {
                if (drr["ParentID"].ToString() == dr["ID"].ToString())
                {
                    TreeNode ttn = new TreeNode();
                    ttn.Tag = drr["ID"].ToString();
                    ttn.Text = drr["CategoryName"].ToString();
                    tn.Nodes.Add(ttn);
                }
            }
        }

        /// <summary>
        /// 树目录搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tree_Role_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (Convert.ToInt32(e.Node.Tag) > 2)
            {
                Dgv_list.DataSource = bookManagementBLL.getBookCategoryID(e.Node.Tag.ToString());
            }
        }
        #endregion


        #region 图书的增删改查
        /// <summary>
        /// 在datagridview中添加button按钮
        /// </summary>
        private void DataGridViewButton()
        {
            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            btn.Name = "Modify";
            btn.HeaderText = "修改信息";
            btn.DefaultCellStyle.NullValue = "修改";
            Dgv_list.Columns.Add(btn);

            DataGridViewButtonColumn btn2 = new DataGridViewButtonColumn();
            btn2.Name = "del";
            btn2.HeaderText = "操作";
            btn2.DefaultCellStyle.NullValue = "删除";
            Dgv_list.Columns.Add(btn2);
        }

        /// <summary>
        /// 图书修改、删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Dgv_list_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.Dgv_list.Columns[e.ColumnIndex].Name == "Modify")
            {
                //修改图书
                UedatBook();
            }
            else if (this.Dgv_list.Columns[e.ColumnIndex].Name == "del")
            {
                //删除图书
                DeleteBokk();
            }
        }

        /// <summary>
        /// 修改图书实现方法
        /// </summary>
        private void UedatBook()
        {
            int index = Dgv_list.SelectedCells[0].RowIndex;
            var Rows = Dgv_list.Rows[index].Cells["图书id"].Value;
            var id = Dgv_list.Rows[index].Cells["编号"].Value;
            if (Rows.ToString()!="" && id.ToString()!="")
            {
                DialogResult dt = new AmendPage(Rows.ToString(), Convert.ToInt32(id)).ShowDialog();
                if (dt == DialogResult.OK)
                {
                    Dgv_list.DataSource = bookManagementBLL.getBookbyid("");
                }
            }
           
        }

        /// <summary>
        /// 删除图书实现方法
        /// </summary>
        private void DeleteBokk()
        {
            int index = Dgv_list.SelectedCells[0].RowIndex;
            var aa = Dgv_list.Rows[index].Cells["图书id"].Value;
            var bookname = Dgv_list.Rows[index].Cells["图书名称"].Value;
            var ID = Dgv_list.Rows[index].Cells["编号"].Value;
            if (MessageBox.Show(" 确认删除：‘" + bookname + "’ 这本书？", " 删除 ", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string str= bookManagementBLL.DeleteBook(aa.ToString(), ID.ToString());
                if (str!= EnumTools.GetDescription(ManageEnums.DOCUMENT.Datate_SUCCEED))
                {
                    MessageBox.Show(str);
                    //刷新列表
                    Dgv_list.DataSource = Paging.Thequery3(PageDto);
                }
                else
                {
                    MessageBox.Show(str);
                }
            }
        }

        /// <summary>
        /// 根据bookid搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_search_Click(object sender, EventArgs e)
        {
            //刷新 
            Dgv_list.DataSource = bookManagementBLL.getBookbyid(txt_search.Text);

        }

        /// <summary>
        /// 添加图书
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_AddBookFrom_Click(object sender, EventArgs e)
        {
            DialogResult dt = new AddPage().ShowDialog();
            if (dt == DialogResult.OK)
            {
                Dgv_list.DataSource = bookManagementBLL.getBookbyid("");
            }
        }

        #endregion


    }
}
